# Customize this file, documentation can be found here:
# https://docs.fastlane.tools/actions/
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
min_fastlane_version("2.105.2")

default_platform(:ios)

platform :ios do
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
        # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    ENV["FASTLANE_DONT_STORE_PASSWORD"] = "1"
    ENV["FASTLANE_USER"] = "peiqidu@163.com"
    ENV["FASTLANE_PASSWORD"] = "Qwer.123"
    ENV["MATCH_PASSWORD"] = "Qwer.123"
    cocoapods
    # cocoapods
  end

  desc "Runs all the tests"
  lane :test do
    run_tests
  end

  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :beta do |options|
    register_devices(devices_file: "./fastlane/devices.txt")
    match(git_url: "git@192.168.1.8:Mobile/iOS/026Profile.git",
      type: "adhoc",
      app_identifier: "com.merchant.zxzx026.beta",
      username: "peiqidu@163.com",
      force_for_new_devices:true
    )
    scheme = "ZXZX"
    # increment_version_number(version_number: options[:version]) #根据入参version获取app版本号
    # increment_build_number(build_number: options[:build])  #根据入参build设置build number
    #自动增加build号
	  if options[:version]
      version = options[:version]
      build = options[:build]
      increment_version_number(version_number: version)
      increment_build_number(build_number: build)
    else
      increment_build_number
      version = get_info_plist_value(path: "./#{scheme}/Classes/Supporting\ Files/Info.plist", key: "CFBundleShortVersionString") 
      build = get_info_plist_value(path: "./#{scheme}/Classes/Supporting\ Files/Info.plist", key: "CFBundleVersion") 
    end
    
    gym(
      scheme: "#{scheme}", 
      workspace: "#{scheme}.xcworkspace",
      configuration: "Debug", # 默认Release
      silent: true,
      clean: true,
      buildlog_path: "./fastlane/logs",  # fastlane构建ipa的日志输出目录
      output_name: "zxzx_026_#{version}.#{build}.ipa",
      export_method: "ad-hoc",
      export_options: {
        signingStyle: "manual",
        method: "ad-hoc",
        provisioningProfiles: {
          "com.merchant.zxzx026.beta" => "match AdHoc com.merchant.zxzx026.beta"
        }
      }
      ) # more options available
    #upload_to_testflight

    # sh "your_script.sh"
    # You can also use other beta testing services here (run `fastlane actions`)
  end

  desc "Deploy a new version to the App Store"
  lane :release do
    # sync_code_signing(type: "appstore")
    capture_screenshots
    build_app(scheme: "ZXZX") # more options available
    #upload_to_app_store(force: true)
    # frame_screenshots
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://docs.fastlane.tools/#metrics
